<?php namespace App\Core\Service;

use DB;
use App\Core\Model;
use App\Core\Metadata\Meta;
use App\Core\Repository\Repository;
use App\Core\Metadata\Data\FormData;
use App\Core\Metadata\Validator;

trait Service{
    
    use Listenable;
    
    protected $debug = true;//调试模式
    
    protected $formData;
    protected $validator;
    
    protected $repositorys = [];
    protected $metas = [];
    protected $models = [];
    
    public static $STATUS_SUCCESS = '0';
    public static $STATUS_SUCCESS_MSG = '成功';
    public static $STATUS_INPUT_ERROR = '1';
    public static $STATUS_INPUT_ERROR_MSG = '参数有误';
    public static $STATUS_SYS_ERROR = '2';
    public static $STATUS_SYS_ERROR_MSG = '系统错误';
    
    public function bootBaseService(){
        $this->formData = new FormData();
        $this->validator = new Validator;
        if($this->debug){
            $this->enableQueryLog();
        }
    }
    
    //---------------------getter/setter-----------------------

    public function setRepository(Repository $repository, $name = 'repository'){
        $this->repositorys[$name] = $repository;
        return $this;
    }
    
    public function getRepository($name = 'repository'){
        if(!isset($this->repositorys[$name])){
            throw new \Exception('parameter not validated');
        }
        return $this->repositorys[$name];
    }
    
    public function setMeta(Meta $meta, $name = 'meta'){
        $this->metas[$name] = $meta;
        return $this;
    }
    
    public function getMeta($name = 'meta'){
        if(!isset($this->metas[$name])){
            throw new \Exception('parameter not validated');
        }
        return $this->metas[$name];
    }
    
    public function setModel(Model $model, $name = 'model'){
        $this->models[$name] = $model;
        return $this;
    }
    
    public function getModel($name = 'model'){
        if(!isset($this->models[$name])){
            throw new \Exception('Model nameed '.$name.' not fond in Service');
        }
        return $this->models[$name];
    }
    
    public function getFormData(){
        return $this->formData;
    }
    
    //---------------------http响应-----------------------
    
    public function success($data = ''){
        return $this->toJson([
            'status' => static::$STATUS_SUCCESS,
            'msg' => static::$STATUS_SUCCESS_MSG,
            'data' => $data
        ]);
    }
    
    public function error($status, $msg){
        return $this->toJson([
            'status' => $status,
            'msg' => $msg
        ]);
    }
    
    public function inputError($msg = ''){
        if($this->debug && $msg instanceof \Exception){
            throw $msg;
        }
        return $this->error(static::$STATUS_INPUT_ERROR, $msg && is_string($msg) ? $msg : static::$STATUS_INPUT_ERROR_MSG);
    }
    
    public function sysError($msg = ''){
        if($this->debug && $msg instanceof \Exception){
            throw $msg;
        }
        return $this->error(static::$STATUS_SYS_ERROR, $msg && is_string($msg) ? $msg : static::$STATUS_SYS_ERROR_MSG);
    }
    
    public function toJson($data){
		return json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
	}
    
    //开启查询日志
    public function enableQueryLog(){
        DB::connection()->enableQueryLog();
    }
    
    //打印/返回查询日志
    public function queryLog($return = false){
        $logs = DB::getQueryLog();
        if($return){
            return $logs;
        }
        echo '<pre>';
        print_r($logs);
    }
}